package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysGrade;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.GradeService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.GradeVo;
import com.github.pagehelper.PageInfo;

/**
 * 年级管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/grade", name = "年级管理")
public class GradeController {
	
	@Autowired
	private GradeService  gradeService;
	
	/**
	 *
	 * @apiGroup grade
	 * @api {get} /grade/listPage 获取年级列表(分页)
	 * @apiVersion 1.0.0
	 * @apiName getGradeListPage
	 *
	 * @apiParam {Long} phaseId 学段id
	 * @apiParam {Integer} pageNum 页码
	 * @apiParam {Integer} pageSize 每页行数
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "total": 3,
		        "list": [
		            {
		                "gradeId": 7,
		                "gradeName": "七年级",
		                "phaseId": 2,
		                "yearNum": 1,
		                "phaseName": "初中"
		            },
		            {
		                "gradeId": 8,
		                "gradeName": "八年级",
		                "phaseId": 2,
		                "yearNum": 2,
		                "phaseName": "初中"
		            },
		            {
		                "gradeId": 9,
		                "gradeName": "九年级",
		                "phaseId": 2,
		                "yearNum": 3,
		                "phaseName": "初中"
		            }
		        ],
		        "pageNum": 1,
		        "pageSize": 10,
		        "size": 3,
		        "startRow": 1,
		        "endRow": 3,
		        "pages": 1,
		        "prePage": 0,
		        "nextPage": 0,
		        "isFirstPage": true,
		        "isLastPage": true,
		        "hasPreviousPage": false,
		        "hasNextPage": false,
		        "navigatePages": 8,
		        "navigatepageNums": [
		            1
		        ],
		        "navigateFirstPage": 1,
		        "navigateLastPage": 1,
		        "firstPage": 1,
		        "lastPage": 1
		    }
		}
	 *
	 *
	 */
    @RequestMapping(value = "listPage", name = "年级列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/grade/listPage')")
    public RestResult getGradeListPage(@RequestParam(name = "phaseId", required = false) Long phaseId,
    		@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) throws Exception {

    	PageInfo<GradeVo> pageInfo = gradeService.getGradeListPage(phaseId, pageNum, pageSize);
        return RestResult.ok(pageInfo);
    }
    
    /**
	 *
	 * @apiGroup grade
	 * @api {get} /grade/list 根据学段获取年级列表
	 * @apiVersion 1.0.0
	 * @apiName getGradeList
	 *
	 * @apiParam {Long} phaseId 学段id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "gradeId": 7,
		            "gradeName": "七年级",
		            "phaseId": 2,
		            "yearNum": 1,
		            "phaseName": "初中"
		        },
		        {
		            "gradeId": 8,
		            "gradeName": "八年级",
		            "phaseId": 2,
		            "yearNum": 2,
		            "phaseName": "初中"
		        },
		        {
		            "gradeId": 9,
		            "gradeName": "九年级",
		            "phaseId": 2,
		            "yearNum": 3,
		            "phaseName": "初中"
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "年级列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/grade/list')")
    public RestResult getGradeList(Long phaseId) throws Exception {

        List<GradeVo> gradeList = gradeService.getGradeList(phaseId);
        return RestResult.ok(gradeList);
    }
    
    /**
	 *
	 * @apiGroup grade
	 * @api {post} /grade/save 保存年级信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysGrade} grade 班级信息
	 * @apiParamExample 请求示例（json）
	 * {
		
			"gradeId": 2,
			"gradeName": "八年级",
		    "phaseId": "2"
		
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "gradeId": 2
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "save", name = "保存年级信息", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/grade/save')")
	public RestResult save(@Valid @RequestBody SysGrade grade) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = gradeService.saveGrade(grade);
	
		return RestResult.ok(result);
	}
	

	/**
	 *
	 * @apiGroup grade
	 * @api {get} /grade/info 获取年级信息
	 * @apiVersion 1.0.0
	 * @apiName getGrade
	 *
	 * @apiParam {Long} gradeId 班级id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "gradeId": 8,
		        "gradeName": "八年级",
		        "phaseId": 2,
		        "yearNum": 2,
		        "phaseName": "初中"
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "年级信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/grade/info')")
	public RestResult getGrade(@RequestParam Long gradeId) throws Exception{

		GradeVo gradeVo = gradeService.getGradeById(gradeId);
	
		return RestResult.ok(gradeVo);
	}
	
	/**
	 *
	 * @apiGroup grade
	 * @api {post} /grade/delete 删除年级信息
	 * @apiVersion 1.0.0
	 * @apiName deleteGrade
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "gradeId": 2
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/delete" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/grade/delete')")
    public RestResult deleteGrade(@RequestBody Map<String,Object> dataMap) throws Exception {
  	   
     	Long gradeId = MapUtils.getLong(dataMap, "gradeId");
     	
     	if (gradeId == null || gradeId<=0)
     	{
     		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
     	}
    	    			
    	int isOk = gradeService.deleteGrade(gradeId);
    	if (isOk>0)
		{
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
    }


}
